// pages/notice/notice.js
var publicApi = require('../../script/public.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    noticeList: [],
    pagination: {
      page: 1,
      rows: 8,
      sidx: '',
      sord: ''
    },
    count: 0,
    canLoadData: true,
    noData: false
  },
  _refreshData: function () {
    this.setData({ //初始化数据
      // noticeList: [],
      pagination: {
        page: 1,
        rows: 8,
        sidx: 'sendTime',
        sord: 'desc'
      },
      count: 0,
      canLoadData: true,
      noData: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // this._refreshData();
    // this.getNoticeList(true)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this._refreshData();
    this.getNoticeList(true)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this._refreshData();
    this.getNoticeList(true)
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.canLoadData) {
      this.setData({
        'pagination.page': this.data.pagination.page + 1
      })
      this.getNoticeList();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 1000
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getNoticeList(isRefresh) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const params = {
      pagination: this.data.pagination,
      queryJson: {
        msgType: "1,2"
      }
    };
    publicApi.GetMsgList(params).then(res => {
      if (res.code === 200) {
        let dataList = []
        if (isRefresh) {
          dataList = [...res.data.Items]
        } else {
          dataList = [...this.data.noticeList, ...res.data.Items]
        }
        let canLoadData = res.data.HasNextPages
        const noData = dataList.length === 0
        this.setData({
          noticeList: dataList,
          count: res.data.TotalCount,
          canLoadData: canLoadData,
          noData: noData
        })
      }
      wx.hideLoading();
    }).catch(_err => {
      wx.hideLoading();
    });
  },
  toNoticeDetail(e) {
    const notice = e.currentTarget.dataset.notice
    if(!notice.isNotice) return;
    const businessId = notice.businessId
    const msgStatus = notice.msgStatus
    if (businessId) {
      if (msgStatus === 0) {
        wx.navigateTo({
          url: "../notice-detail/notice-detail?businessId=" + businessId + "&msgId=" + notice.msgId
        })
      } else {
        wx.navigateTo({
          url: "../notice-detail/notice-detail?businessId=" + businessId
        })
      }
    }
  },
  clean() {
    publicApi.ChangeAllStatus({
      msgType: "1,2"
    }).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: "全部已读！",
          icon: 'success',
          duration: 2000
        })
        this.getNoticeList(true);
      }
    })
  }
})